package com.cn.gxa.redisutils.service;

import com.cn.gxa.redisutils.bean.User;
import com.cn.gxa.redisutils.dao.RedisUtil;
import com.cn.gxa.redisutils.dao.UserDao;
import com.cn.gxa.redisutils.util.JWTUtil;
import com.cn.gxa.redisutils.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;
    //@Autowired
    //private RedisTemplate redisTemplate;
    @Autowired
    private RedisUtil  redisUtil;

    /**
     * 查询token是否存在
     * @param token
     * @return
     */
    public  boolean checkToken(String  token){
        if(redisUtil.hasKey(token)){
            //延时
            redisUtil.expire(token,30*3);
            return  true;
        }
        return  false;
    }

   // @Transactional
    public String login(String name, String password) {
        User user = userDao.findByUsernameAndPassword(name, password);
        String token = null;
        if (user != null) {
            //生成token  保证唯一性
           // token = JWTUtil.sign(user, 1000 * 60 * 30);
            //token= MD5Utils.md5(user.toString(),1024);
           // token= UUID.randomUUID().toString().replace("-","");
            token=MD5Utils.md5(System.currentTimeMillis()+user.toString());
            redisUtil.set(token,user,30*3);
            //存入redis  设置有效时间
           // redisTemplate.opsForValue().set(token, user);
        }

        return token;
        // key:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjY4NTgyMDk3NzMsInBheWxvYWQiOiJ7XCJub1wiOjE4LFwidXNlcm5hbWVcIjpcIjFcIixcInBhc3N3b3JkXCI6XCIxXCIsXCJuaWNrXCI6bnVsbCxcImxvZ2ludGltZVwiOm51bGwsXCJhdXRoXCI6XCI3XCIsXCJiaXJ0aGRheVwiOm51bGx9In0.nVF8y789ZN83V-9pAaBKW6eH42lhfurVx7sI9y9BNAI
        //value  :对象
    }

    public List<User> findAll() {
       List<User> list = (List) redisUtil.get("allUser");
        //在redis中没有
        if (list == null) {
            list = userDao.findAll();
            //存入redis中
            redisUtil.set("allUser", list,20);
        }
        return list;

    }


    /**
     *
     * @param page
     * @param limit
     * @return
     */
    public  List<User>  findAll(Integer page,Integer limit){
       // List<User>  list=null;
        //  先判断是否存在集合数据
        if(!redisUtil.hasKey("users")){
            List<Integer>  ids = userDao.findAllIds();
            List<User>  userList = userDao.findAll();
            //一个是 sortset 只存入 id
            redisUtil.addSortSet("users",ids);
            redisUtil.expire("users",30);
            for (User user:userList){
                redisUtil.set(user.getNo()+"",user,30);
            }
            //查询所有的id
         // List  ids=userDao.findIds();
            //  一个String  id: 对象
        }//2 5
        int  start=(page-1)*limit;//10 1  index   (page-1)*limit+1    score  6
        int  end=start+limit-1;//20  5            start+limi-1      10
        //获得 10-20 的id
        Set<Object>  set= redisUtil.lGetSortSet("users",start,end);
        // //  用id 查询 所有商品
        List<User>  list=redisUtil.multiGet(set);

        return   list;
    }
}
